package com.controller;

import com.entry.Result;
import com.entry.Shopping;
import com.server.ShoppingService;
import com.util.RedisUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class CartController {

    @Resource
    ShoppingService shoppingService;
    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/product/getshoppingList")
    public String getshoppingList(HttpSession session){
        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        List<Shopping> shoppingList= shoppingService.getShoppingList(member_id);

        int countShopping=shoppingService.getCount(member_id);
        session.setAttribute("countShopping",countShopping);

        session.setAttribute("shoppingList",shoppingList);
        shoppingList.forEach(System.out::println);
        return "cart";
    }

    //更新商品信息
    @RequestMapping(value = "/cart/updateCartProduct",method = RequestMethod.GET)
    @ResponseBody
    public Result updateCartProduct(@Param("product_id") int product_id,@Param("shopping_product_amount") int shopping_product_amount,HttpSession session){
        Result result = new Result();
        System.out.println("更新单个商品product_id"+product_id);
        System.out.println("更新单个商品shopping_product_amount"+shopping_product_amount);

        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        System.out.println("更新单个商品member_id"+member_id);
        int count=shoppingService.updateCartCount(member_id, product_id, shopping_product_amount);

        if (count>0){
            int countShopping=shoppingService.getCount(member_id);
            session.setAttribute("countShopping",countShopping);
            List<Shopping> shoppingList= shoppingService.getShoppingList(member_id);
            session.setAttribute("shoppingList",shoppingList);
            System.out.println("更新单个商品成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }


    //更新商品信息是否勾选
    @RequestMapping(value = "/cart/updateCartChecked",method = RequestMethod.GET)
    @ResponseBody
    public Result updateCartChecked(@Param("product_id") int product_id,@Param("shopping_checked")int shopping_checked,HttpSession session){
        Result result = new Result();

        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));

        int count=shoppingService.updateCartChecked(product_id, member_id,shopping_checked);

        if (count>0){
            System.out.println("更新商品勾选成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    //更新商品信息是否勾选
    @RequestMapping(value = "/cart/updateCartCheckedAll",method = RequestMethod.GET)
    @ResponseBody
    public Result updateCartCheckedAll(@Param("shopping_checked")int shopping_checked,HttpSession session){
        Result result = new Result();

        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));

        int count=shoppingService.updateCartChecked(0, member_id,shopping_checked);

        if (count>0){
            System.out.println("更新商品勾选成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }


    //删除单个商品
    @RequestMapping(value = "/cart/delcartproduct",method = RequestMethod.GET)
    @ResponseBody
    public Result delcartproduct(@Param("product_id") int product_id,HttpSession session){
        Result result = new Result();
        System.out.println("删除单个商品product_id"+product_id);

        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        System.out.println("删除单个商品member_id"+member_id);
        int count=shoppingService.delCartProduct(member_id, product_id);
        if (count>0){
            List<Shopping> shoppingList= shoppingService.getShoppingList(member_id);
            session.setAttribute("shoppingList",shoppingList);
            int countShopping=shoppingService.getCount(member_id);
            session.setAttribute("countShopping",countShopping);
            System.out.println("商品删除成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }
    //删除多个商品
    @RequestMapping(value = "/cart/deleteCartChecked",method = RequestMethod.GET)
    @ResponseBody
    public Result deleteCartChecked(/*@Param("product_id") int product_id,*/HttpSession session){
        Result result = new Result();
        //System.out.println("删除多个商品product_id"+product_id);

        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        //System.out.println("删除多个商品member_id"+member_id);
        //int count=shoppingService.delCartProduct(member_id, product_id);
        int count=shoppingService.delCartProducts(member_id);
        if (count>0){
            List<Shopping> shoppingList= shoppingService.getShoppingList(member_id);
            session.setAttribute("shoppingList",shoppingList);
            int countShopping=shoppingService.getCount(member_id);
            session.setAttribute("countShopping",countShopping);
            System.out.println("商品删除成功");
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    //清空购物车
    @RequestMapping(value = "/cart/delCartProductById",method = RequestMethod.GET)
    @ResponseBody
    public Result delCartProductById(HttpSession session){
        Result result = new Result();

        int member_id=Integer.parseInt(String.valueOf(redisUtil.get("member_id")));
        int count=shoppingService.delCartProductById(member_id);
        if (count>0){
            int countShopping=shoppingService.getCount(member_id);
            session.setAttribute("countShopping",countShopping);
            System.out.println("购物车清空成功");
            result.setSuccess(true);
            return result;
        }
        System.out.println("购物车清空失败");
        result.setSuccess(false);
        return result;
    }


    //添加商品
    @RequestMapping(value = "/cart/addcart",method = RequestMethod.GET)
    @ResponseBody
    public Result addcart(int product_id, int shopping_product_amount, HttpSession session){
        Result result = new Result();
        String member_id=String.valueOf(redisUtil.get("member_id"));
        System.out.println("product_id="+product_id);
        System.out.println("shopping_product_amount="+shopping_product_amount);

        //根据redies缓存的member_id判断用户
        if (member_id!=null){
            int id= Integer.parseInt(member_id);
            System.out.println("id="+id);
            Shopping shopping=shoppingService.getCountById(id, product_id);

            if (shopping!=null){
                int number=shopping.getShopping_product_amount()+shopping_product_amount;
                System.out.println("商品数量成功更新，数量为："+number);
                //购物车有此类商品，则进行商品数量的更新
                int count=shoppingService.updateCartCount(id, product_id, number);
                if (count>0){
                    System.out.println("商品数量成功更新");
                    result.setSuccess(true);
                    return result;
                }
            }else {
                //购物车没有此类商品，进行购物车的商品添加
                int count=shoppingService.addCart(id,product_id,shopping_product_amount);
                System.out.println("添加商品成功数量："+count);
                if (count>0){
                    int countShopping=shoppingService.getCount(id);
                    session.setAttribute("countShopping",countShopping);
                    System.out.println("添加商品成功");
                    result.setSuccess(true);
                    return result;
                }
            }



        }
        result.setSuccess(false);
        return result;
    }
}
